<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>Go爬虫 | lilei</title>
    <meta name="generator" content="VuePress 1.8.2">
    <link rel="icon" href="/images/icons/logo.png">
    <link rel="apple-touch-icon" href="/images/icons/logo.png">
    <link rel="manifest" href="/manifest.json">
    <script type="text/javascript" src="/assets/js/load.js"></script>
    <meta name="description" content="个人网站">
    <meta name="twitter:title" content="Go爬虫">
    <meta name="twitter:description" content="">
    <meta name="twitter:card" content="summary">
    <meta name="twitter:url" content="http://www.lileiblog.xyz/Form/Go%E7%88%AC%E8%99%AB.html">
    <meta property="og:type" content="article">
    <meta property="og:title" content="Go爬虫">
    <meta property="og:description" content="">
    <meta property="og:url" content="http://www.lileiblog.xyz/Form/Go%E7%88%AC%E8%99%AB.html">
    <meta property="og:site_name" content="lilei">
    <meta itemprop="name" content="Go爬虫">
    <meta itemprop="description" content="">
    <meta name="theme-color" content="#3eaf7c">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="msapplication-TileImage" content="/images/icons/logo.png">
    <meta name="msapplication-TileColor" content="#ffffff">
    <meta name="keywords" content="lilei,vue,vuepress,vuepress-theme,theme,主题,vuepress主题,blog
        ,vuepress-blog,java,python,shell,sql,golang,script,shell script,nginx,windows,去广告,androd,google drive
        ,one drive,idea,eclipse,git,小程序">
    <meta name="referrer" content="never">
    
    <link rel="preload" href="/assets/css/0.styles.8932179b.css" as="style"><link rel="preload" href="/assets/js/app.269009d6.js" as="script"><link rel="preload" href="/assets/js/3.974b9720.js" as="script"><link rel="preload" href="/assets/js/9.ad652cfe.js" as="script"><link rel="prefetch" href="/assets/js/10.5915e99b.js"><link rel="prefetch" href="/assets/js/11.ff732762.js"><link rel="prefetch" href="/assets/js/12.8bb86d91.js"><link rel="prefetch" href="/assets/js/13.18bde5b0.js"><link rel="prefetch" href="/assets/js/14.314fae34.js"><link rel="prefetch" href="/assets/js/15.0a4ff48f.js"><link rel="prefetch" href="/assets/js/16.ffaf1503.js"><link rel="prefetch" href="/assets/js/17.49a31a6e.js"><link rel="prefetch" href="/assets/js/18.7e6443bf.js"><link rel="prefetch" href="/assets/js/19.6880cc9a.js"><link rel="prefetch" href="/assets/js/20.1fb3f255.js"><link rel="prefetch" href="/assets/js/21.0cd39eea.js"><link rel="prefetch" href="/assets/js/22.392d85cb.js"><link rel="prefetch" href="/assets/js/23.f50adb15.js"><link rel="prefetch" href="/assets/js/24.43e324e1.js"><link rel="prefetch" href="/assets/js/25.e3663205.js"><link rel="prefetch" href="/assets/js/26.da141fb8.js"><link rel="prefetch" href="/assets/js/27.6c05235a.js"><link rel="prefetch" href="/assets/js/28.c124245a.js"><link rel="prefetch" href="/assets/js/29.d185870d.js"><link rel="prefetch" href="/assets/js/30.8587d422.js"><link rel="prefetch" href="/assets/js/31.dc382b56.js"><link rel="prefetch" href="/assets/js/32.20ba32e9.js"><link rel="prefetch" href="/assets/js/33.6ecd7de9.js"><link rel="prefetch" href="/assets/js/34.2b91c13a.js"><link rel="prefetch" href="/assets/js/35.66ed0733.js"><link rel="prefetch" href="/assets/js/36.ce7bca84.js"><link rel="prefetch" href="/assets/js/37.d00bb5ac.js"><link rel="prefetch" href="/assets/js/38.c3ecbb6f.js"><link rel="prefetch" href="/assets/js/39.308f4175.js"><link rel="prefetch" href="/assets/js/4.520f6ecc.js"><link rel="prefetch" href="/assets/js/40.f4b98bc2.js"><link rel="prefetch" href="/assets/js/41.95fae64f.js"><link rel="prefetch" href="/assets/js/42.fe4f877b.js"><link rel="prefetch" href="/assets/js/43.ca8f222e.js"><link rel="prefetch" href="/assets/js/44.4d4ee79e.js"><link rel="prefetch" href="/assets/js/45.79178b33.js"><link rel="prefetch" href="/assets/js/46.c3817d0b.js"><link rel="prefetch" href="/assets/js/47.b8252443.js"><link rel="prefetch" href="/assets/js/48.df9d5d3b.js"><link rel="prefetch" href="/assets/js/49.d812fdb2.js"><link rel="prefetch" href="/assets/js/5.f1a24a16.js"><link rel="prefetch" href="/assets/js/50.55cfcaa8.js"><link rel="prefetch" href="/assets/js/51.b8608b02.js"><link rel="prefetch" href="/assets/js/52.52c5e00d.js"><link rel="prefetch" href="/assets/js/53.3a9bd5fb.js"><link rel="prefetch" href="/assets/js/54.a4240c13.js"><link rel="prefetch" href="/assets/js/55.01c4b876.js"><link rel="prefetch" href="/assets/js/56.f29549a3.js"><link rel="prefetch" href="/assets/js/57.01438a04.js"><link rel="prefetch" href="/assets/js/58.29bb4626.js"><link rel="prefetch" href="/assets/js/59.e8b89408.js"><link rel="prefetch" href="/assets/js/6.8c478b39.js"><link rel="prefetch" href="/assets/js/60.bedb5381.js"><link rel="prefetch" href="/assets/js/61.bdfbe584.js"><link rel="prefetch" href="/assets/js/62.cb825d1a.js"><link rel="prefetch" href="/assets/js/63.a9516572.js"><link rel="prefetch" href="/assets/js/64.aa854ed7.js"><link rel="prefetch" href="/assets/js/65.285f883d.js"><link rel="prefetch" href="/assets/js/66.12849f37.js"><link rel="prefetch" href="/assets/js/67.c18b00e4.js"><link rel="prefetch" href="/assets/js/68.fa59daa9.js"><link rel="prefetch" href="/assets/js/69.e9d5dda4.js"><link rel="prefetch" href="/assets/js/7.cfb0d0ea.js"><link rel="prefetch" href="/assets/js/70.db126cce.js"><link rel="prefetch" href="/assets/js/71.6acb04cd.js"><link rel="prefetch" href="/assets/js/72.7ff0ab11.js"><link rel="prefetch" href="/assets/js/73.9a66da79.js"><link rel="prefetch" href="/assets/js/74.0a38a030.js"><link rel="prefetch" href="/assets/js/75.7b32dbb3.js"><link rel="prefetch" href="/assets/js/8.cb9c08e5.js"><link rel="prefetch" href="/assets/js/vendors~flowchart.110a1065.js">
    <link rel="stylesheet" href="/assets/css/0.styles.8932179b.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><img src="/images/icons/logo.png" alt="lilei" class="logo"> <span class="site-name can-hide">lilei</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/Form/" class="nav-link router-link-active">
  Form
</a></div><div class="nav-item"><a href="/IDE/" class="nav-link">
  IDE
</a></div><div class="nav-item"><a href="/Java/" class="nav-link">
  Java
</a></div><div class="nav-item"><a href="/Other/" class="nav-link">
  Other
</a></div><div class="nav-item"><a href="/Python/" class="nav-link">
  Python
</a></div><div class="nav-item"><a href="/SQL/" class="nav-link">
  SQL
</a></div><div class="nav-item"><a href="/Shell/" class="nav-link">
  Shell
</a></div><div class="nav-item"><a href="/System/" class="nav-link">
  System
</a></div> <a href="https://github.com/woytu/notes-vuepress" target="_blank" rel="noopener noreferrer" class="repo-link">
    GitHub
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/Form/" class="nav-link router-link-active">
  Form
</a></div><div class="nav-item"><a href="/IDE/" class="nav-link">
  IDE
</a></div><div class="nav-item"><a href="/Java/" class="nav-link">
  Java
</a></div><div class="nav-item"><a href="/Other/" class="nav-link">
  Other
</a></div><div class="nav-item"><a href="/Python/" class="nav-link">
  Python
</a></div><div class="nav-item"><a href="/SQL/" class="nav-link">
  SQL
</a></div><div class="nav-item"><a href="/Shell/" class="nav-link">
  Shell
</a></div><div class="nav-item"><a href="/System/" class="nav-link">
  System
</a></div> <a href="https://github.com/woytu/notes-vuepress" target="_blank" rel="noopener noreferrer" class="repo-link">
    GitHub
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav>  <ul class="sidebar-links"><li><section class="sidebar-group depth-0"><p class="sidebar-heading open"><span>Form</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/Form/" aria-current="page" class="sidebar-link">介绍</a></li><li><a href="/Form/Go操作Windows.html" class="sidebar-link">Go操作Windows</a></li><li><a href="/Form/Go爬虫.html" class="active sidebar-link">Go爬虫</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/Form/Go爬虫.html#flag" class="sidebar-link">Flag</a></li><li class="sidebar-sub-header"><a href="/Form/Go爬虫.html#html解析" class="sidebar-link">HTML解析</a></li><li class="sidebar-sub-header"><a href="/Form/Go爬虫.html#chromedp" class="sidebar-link">chromedp</a></li><li class="sidebar-sub-header"><a href="/Form/Go爬虫.html#godet" class="sidebar-link">godet</a></li><li class="sidebar-sub-header"><a href="/Form/Go爬虫.html#gcd" class="sidebar-link">gcd</a></li></ul></li><li><a href="/Form/Go笔记.html" class="sidebar-link">Go笔记</a></li><li><a href="/Form/Go编译打包.html" class="sidebar-link">Go编译打包</a></li></ul></section></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><h1 id="go爬虫"><a href="#go爬虫" class="header-anchor">#</a> Go爬虫</h1> <p></p><div class="table-of-contents"><ul><li><a href="#flag">Flag</a></li><li><a href="#html解析">HTML解析</a></li><li><a href="#chromedp">chromedp</a></li><li><a href="#godet">godet</a></li><li><a href="#gcd">gcd</a></li></ul></div><p></p> <h2 id="flag"><a href="#flag" class="header-anchor">#</a> Flag</h2> <p><strong><a href="/Web/#xpath和css选择器">xpath和css选择器</a></strong></p> <p><strong><a href="/Shell/Python爬虫.html#headlessbrowser">HeadlessBrowser</a></strong></p> <ul><li>类似于BeautifulSoup <a href="https://github.com/anaskhan96/soup" target="_blank" rel="noopener noreferrer">https://github.com/anaskhan96/soup<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li>基于DevTools协议的高级驱动程序 <a href="https://github.com/go-rod/rod" target="_blank" rel="noopener noreferrer">https://github.com/go-rod/rod<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li><a href="https://github.com/derekparker/delve" target="_blank" rel="noopener noreferrer">https://github.com/derekparker/delve<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul> <h2 id="html解析"><a href="#html解析" class="header-anchor">#</a> HTML解析</h2> <ul><li><a href="https://github.com/antchfx" target="_blank" rel="noopener noreferrer">https://github.com/antchfx<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a> htmlquery使用xpath选择器</li> <li><a href="https://github.com/PuerkitoBio/goquery" target="_blank" rel="noopener noreferrer">https://github.com/PuerkitoBio/goquery<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a> goquery使用css选择器</li> <li><a href="https://github.com/gocolly/colly" target="_blank" rel="noopener noreferrer">https://github.com/gocolly/colly<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul> <h2 id="chromedp"><a href="#chromedp" class="header-anchor">#</a> chromedp</h2> <ul><li><a href="https://github.com/chromedp" target="_blank" rel="noopener noreferrer">https://github.com/chromedp<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul> <p><strong>该组织下有以下项目</strong></p> <ul><li><code>https://github.com/chromedp/chromedp</code></li></ul> <blockquote><p><a href="https://godoc.org/github.com/chromedp/chromedp" target="_blank" rel="noopener noreferrer">https://godoc.org/github.com/chromedp/chromedp<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p></blockquote> <ul><li><code>https://github.com/chromedp/cdproto-gen</code></li></ul> <blockquote><p>生成用于<a href="https://chromedevtools.github.io/devtools-protocol" target="_blank" rel="noopener noreferrer">Chrome DevTools<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a>协议的命令，事件和类型的Go代码</p></blockquote> <ul><li><code>https://github.com/chromedp/cdproto</code></li></ul> <blockquote><p>包含针对<a href="https://chromedevtools.github.io/devtools-protocol" target="_blank" rel="noopener noreferrer">Chrome DevTools<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a>协议域生成的命令，类型和事件</p></blockquote> <blockquote><p><a href="https://godoc.org/github.com/chromedp/cdproto" target="_blank" rel="noopener noreferrer">https://godoc.org/github.com/chromedp/cdproto<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p></blockquote> <p><strong><code>cdproto</code>下有以下包</strong></p> <ul><li><code>github.com/chromedp/cdproto/cdp</code></li></ul> <blockquote><p><code>cdp.TimeSinceEpoch(time.Now().Add(180 * 24 * time.Hour))</code></p></blockquote> <ul><li><code>github.com/chromedp/cdproto/network</code> 网络处理</li></ul> <blockquote><p>比如启用启用网络跟踪，现在将网络事件传递给客户端<code>network.Enable()</code></p></blockquote> <ul><li><code>github.com/chromedp/cdproto/page</code> chrome页面处理</li></ul> <blockquote><p>比如设置下载<code>page.SetDownloadBehavior(page.SetDownloadBehaviorBehaviorDeny)</code></p></blockquote> <ul><li><code>github.com/chromedp/cdproto/target</code> 操作标签页 <a href="https://chromedevtools.github.io/devtools-protocol/tot/Target#method-createTarget" target="_blank" rel="noopener noreferrer">Target.createTarget<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul> <div class="language-go extra-class"><pre class="language-go"><code><span class="token comment">// 根据url新建浏览器标签页</span>
tag <span class="token operator">:=</span> target<span class="token punctuation">.</span><span class="token function">CreateTarget</span><span class="token punctuation">(</span>url<span class="token punctuation">)</span>
<span class="token comment">// 新建浏览器标签页ID创建上下文，方式一</span>
ctx<span class="token punctuation">,</span> cancel <span class="token operator">=</span> chromedp<span class="token punctuation">.</span><span class="token function">NewContext</span><span class="token punctuation">(</span>ctx<span class="token punctuation">,</span> chromedp<span class="token punctuation">.</span><span class="token function">WithTargetID</span><span class="token punctuation">(</span>target<span class="token punctuation">.</span><span class="token function">ID</span><span class="token punctuation">(</span>tag<span class="token punctuation">.</span>BrowserContextID<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span>

<span class="token comment">// 根据上下文，返回targetID-打开的页面的ID。</span>
tagId<span class="token punctuation">,</span> err <span class="token operator">:=</span> tag<span class="token punctuation">.</span><span class="token function">Do</span><span class="token punctuation">(</span>ctx<span class="token punctuation">)</span>
<span class="token comment">// 新建浏览器标签页ID创建上下文，方式二</span>
ctx<span class="token punctuation">,</span> cancel <span class="token operator">=</span> chromedp<span class="token punctuation">.</span><span class="token function">NewContext</span><span class="token punctuation">(</span>ctx<span class="token punctuation">,</span> chromedp<span class="token punctuation">.</span><span class="token function">WithTargetID</span><span class="token punctuation">(</span>tagId<span class="token punctuation">)</span><span class="token punctuation">)</span>
</code></pre></div><blockquote><p><code>chromedp.Targets(ctx)</code> 获取浏览器实例的标签页信息列表</p></blockquote> <p><strong><code>chromedp</code>能做什么</strong></p> <ul><li><p>反爬虫js，例如有的网页后台js自动发送心跳包，浏览器里会自动运行，不需要我们自动处理</p></li> <li><p>针对于前端页面的自动化测试</p></li> <li><p>解决类似VueJS和SPA之类的渲染</p></li> <li><p>解决网页的懒加载</p></li> <li><p>网页截图和pdf导出，而不需要额外的去学习其他的库实现</p></li> <li><p>seo训练和刷点击量</p></li> <li><p>执行javascript 代码</p></li> <li><p>设置dom的标签属性</p></li></ul> <blockquote><p><code>chromedp</code>同Python的<code>selenium</code>,它是使用<code>Chrome Debugging Protocol</code>(简称cdp) 并且没有外部依赖 (如<code>Selenium</code>, <code>PhantomJS</code>等)</p></blockquote> <ul><li><a href="https://zhangguanzhang.github.io/2019/07/14/chromedp" target="_blank" rel="noopener noreferrer">golang headless browser包chromedp初探<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li><a href="https://mojotv.cn/2018/12/26/chromedp-tutorial-for-golang" target="_blank" rel="noopener noreferrer">go语言chromedp使用教程<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul> <table><thead><tr><th>函数或变量</th> <th>说明</th></tr></thead> <tbody><tr><td>(a *ExecAllocator) Allocate(ctx context.Context, opts ...BrowserOption) (*Browser, error)</td> <td></td></tr> <tr><td>(a *ExecAllocator) Wait()</td> <td></td></tr> <tr><td>(a *RemoteAllocator) Allocate(ctx context.Context, opts ...BrowserOption) (*Browser, error)</td> <td></td></tr> <tr><td>(a *RemoteAllocator) Wait()</td> <td></td></tr> <tr><td>(f ActionFunc) Do(ctx context.Context) error</td> <td></td></tr> <tr><td>(s *Selector) Do(ctx context.Context) error</td> <td></td></tr> <tr><td>(t Tasks) Do(ctx context.Context) error</td> <td></td></tr> <tr><td>After(f func(context.Context, ...*cdp.Node) error) QueryOption</td> <td></td></tr> <tr><td>AtLeast(n int) QueryOption</td> <td></td></tr> <tr><td>Attributes(sel interface{}, attributes *map[string]string, opts ...QueryOption) QueryAction</td> <td></td></tr> <tr><td>AttributesAll(sel interface{}, attributes *[]map[string]string, opts ...QueryOption) QueryAction</td> <td></td></tr> <tr><td>AttributeValue(sel interface{}, name string, value *string, ok *bool, opts ...QueryOption) QueryAction</td> <td></td></tr> <tr><td>Blur(sel interface{}, opts ...QueryOption) QueryAction</td> <td></td></tr> <tr><td>ByFunc(f func(context.Context, *cdp.Node) ([]cdp.NodeID, error)) QueryOption</td> <td></td></tr> <tr><td>ByID(s *Selector)</td> <td>只id来选择元素</td></tr> <tr><td>ByJSPath(s *Selector)</td> <td></td></tr> <tr><td>ByNodeID(s *Selector)</td> <td>按节点ID选择元素</td></tr> <tr><td>ByQuery(s *Selector)</td> <td>根据document.querySelector的规则选择元素，返回单个节点</td></tr> <tr><td>ByQueryAll(s *Selector)</td> <td>根据document.querySelectorAll返回所有匹配的节点</td></tr> <tr><td>BySearch(s *Selector)</td> <td>如果不写，默认会使用这个选择器，类似devtools ctrl+f 搜索</td></tr> <tr><td>Cancel(ctx context.Context) error</td> <td></td></tr> <tr><td>CaptureScreenshot(res *[]byte) Action</td> <td></td></tr> <tr><td>Clear(sel interface{}, opts ...QueryOption) QueryAction</td> <td></td></tr> <tr><td>Click(sel interface{}, opts ...QueryOption) QueryAction</td> <td>触发点击事件</td></tr> <tr><td>CombinedOutput(w io.Writer) ExecAllocatorOption</td> <td></td></tr> <tr><td>ComputedStyle(sel interface{}, style *[]*css.ComputedProperty, opts ...QueryOption) QueryAction</td> <td></td></tr> <tr><td>Dimensions(sel interface{}, model **dom.BoxModel, opts ...QueryOption) QueryAction</td> <td></td></tr> <tr><td>DisableGPU(a *ExecAllocator)</td> <td>关闭gpu,服务器一般没有显卡</td></tr> <tr><td>DoubleClick(sel interface{}, opts ...QueryOption) QueryAction</td> <td></td></tr> <tr><td>Env(vars ...string) ExecAllocatorOption</td> <td>NAME=value形式的通用环境变量列表，用于传递到新的Chrome进程。</td></tr> <tr><td>ExecPath(path string) ExecAllocatorOption</td> <td></td></tr> <tr><td>Flag(name string, value interface{}) ExecAllocatorOption</td> <td></td></tr> <tr><td>Focus(sel interface{}, opts ...QueryOption) QueryAction</td> <td></td></tr> <tr><td>FromContext(ctx context.Context) *Context</td> <td></td></tr> <tr><td>Headless(a *ExecAllocator)</td> <td></td></tr> <tr><td>InnerHTML(sel interface{}, html *string, opts ...QueryOption) QueryAction</td> <td></td></tr> <tr><td>JavascriptAttribute(sel interface{}, name string, res interface{}, opts ...QueryOption) QueryAction</td> <td></td></tr> <tr><td>ListenBrowser(ctx context.Context, fn func(ev interface{}))</td> <td></td></tr> <tr><td>ListenTarget(ctx context.Context, fn func(ev interface{}))</td> <td></td></tr> <tr><td>Location(urlstr *string) Action</td> <td></td></tr> <tr><td>MatchedStyle(sel interface{}, style **css.GetMatchedStylesForNodeReturns, opts ...QueryOption) QueryAction</td> <td></td></tr> <tr><td>Navigate(urlstr string) NavigateAction</td> <td>设置url</td></tr> <tr><td>NavigateBack() NavigateAction</td> <td></td></tr> <tr><td>NavigateForward() NavigateAction</td> <td></td></tr> <tr><td>NavigateToHistoryEntry(entryID int64) NavigateAction</td> <td></td></tr> <tr><td>NavigationEntries(currentIndex *int64, entries *[]*page.NavigationEntry) NavigateAction</td> <td></td></tr> <tr><td>NewContext(parent context.Context, opts ...ContextOption) (context.Context, context.CancelFunc)</td> <td></td></tr> <tr><td>NewExecAllocator(parent context.Context, opts ...ExecAllocatorOption) (context.Context, context.CancelFunc)</td> <td></td></tr> <tr><td>NewRemoteAllocator(parent context.Context, url string) (context.Context, context.CancelFunc)</td> <td></td></tr> <tr><td>NodeEnabled(s *Selector)</td> <td></td></tr> <tr><td>NoDefaultBrowserCheck(a *ExecAllocator)</td> <td>禁用默认浏览器检查。</td></tr> <tr><td>NodeIDs(sel interface{}, ids *[]cdp.NodeID, opts ...QueryOption) QueryAction</td> <td></td></tr> <tr><td>NodeNotPresent(s *Selector)</td> <td></td></tr> <tr><td>NodeNotVisible(s *Selector)</td> <td></td></tr> <tr><td>NodeReady(s *Selector)</td> <td></td></tr> <tr><td>Nodes(sel interface{}, nodes *[]*cdp.Node, opts ...QueryOption) QueryAction</td> <td></td></tr> <tr><td>NodeSelected(s *Selector)</td> <td></td></tr> <tr><td>NodeVisible(s *Selector)</td> <td></td></tr> <tr><td>NoFirstRun(a *ExecAllocator)</td> <td>禁用第一次运行对话框的Chrome命令行选项</td></tr> <tr><td>NoSandbox(a *ExecAllocator)</td> <td>用于禁用沙箱的Chrome命令行选项</td></tr> <tr><td>OuterHTML(sel interface{}, html *string, opts ...QueryOption) QueryAction</td> <td>获取HTML源码</td></tr> <tr><td>ProxyServer(proxy string) ExecAllocatorOption</td> <td></td></tr> <tr><td>Query(sel interface{}, opts ...QueryOption) QueryAction</td> <td></td></tr> <tr><td>QueryAfter(sel interface{}, f func(context.Context, ...*cdp.Node) error, opts ...QueryOption) QueryAction</td> <td></td></tr> <tr><td>Reload() NavigateAction</td> <td></td></tr> <tr><td>RemoveAttribute(sel interface{}, name string, opts ...QueryOption) QueryAction</td> <td></td></tr> <tr><td>Reset(sel interface{}, opts ...QueryOption) QueryAction</td> <td></td></tr> <tr><td>Run(ctx context.Context, actions ...Action) error</td> <td></td></tr> <tr><td>Screenshot(sel interface{}, picbuf *[]byte, opts ...QueryOption) QueryAction</td> <td>标签截图，赋值给变量</td></tr> <tr><td>ScrollIntoView(sel interface{}, opts ...QueryOption) QueryAction</td> <td></td></tr> <tr><td>SendKeys(sel interface{}, v string, opts ...QueryOption) QueryAction</td> <td>使用chromedp.ByID选择器。向标签输入内容。</td></tr> <tr><td>SetAttributes(sel interface{}, attributes map[string]string, opts ...QueryOption) QueryAction</td> <td></td></tr> <tr><td>SetAttributeValue(sel interface{}, name, value string, opts ...QueryOption) QueryAction</td> <td></td></tr> <tr><td>SetJavascriptAttribute(sel interface{}, name, value string, opts ...QueryOption) QueryAction</td> <td></td></tr> <tr><td>SetUploadFiles(sel interface{}, files []string, opts ...QueryOption) QueryAction</td> <td></td></tr> <tr><td>SetValue(sel interface{}, value string, opts ...QueryOption) QueryAction</td> <td></td></tr> <tr><td>Sleep(d time.Duration) Action</td> <td>等待时间</td></tr> <tr><td>Stop() NavigateAction</td> <td></td></tr> <tr><td>Submit(sel interface{}, opts ...QueryOption) QueryAction</td> <td></td></tr> <tr><td>Targets(ctx context.Context) ([]*target.Info, error)</td> <td></td></tr> <tr><td>Text(sel interface{}, text *string, opts ...QueryOption) QueryAction</td> <td></td></tr> <tr><td>TextContent(sel interface{}, text *string, opts ...QueryOption) QueryAction</td> <td></td></tr> <tr><td>Title(title *string) Action</td> <td></td></tr> <tr><td>UserAgent(userAgent string) ExecAllocatorOption</td> <td></td></tr> <tr><td>UserDataDir(dir string) ExecAllocatorOption</td> <td></td></tr> <tr><td>Value(sel interface{}, value *string, opts ...QueryOption) QueryAction</td> <td>取值并赋值给变量</td></tr> <tr><td>WaitEnabled(sel interface{}, opts ...QueryOption) QueryAction</td> <td></td></tr> <tr><td>WaitFunc(wait func(context.Context, *cdp.Frame, ...cdp.NodeID) ([]*cdp.Node, error)) QueryOption</td> <td></td></tr> <tr><td>waitLoaded(ctx context.Context) error</td> <td></td></tr> <tr><td>WaitNewTarget(ctx context.Context, fn func(*target.Info) bool) &lt;-chan target.ID</td> <td></td></tr> <tr><td>WaitNotPresent(sel interface{}, opts ...QueryOption) QueryAction</td> <td></td></tr> <tr><td>WaitNotVisible(sel interface{}, opts ...QueryOption) QueryAction</td> <td></td></tr> <tr><td>WaitReady(sel interface{}, opts ...QueryOption) QueryAction</td> <td></td></tr> <tr><td>WaitSelected(sel interface{}, opts ...QueryOption) QueryAction</td> <td></td></tr> <tr><td>WaitVisible(sel interface{}, opts ...QueryOption) QueryAction</td> <td>等待指定标签元素加载完成</td></tr> <tr><td>WindowSize(width, height int) ExecAllocatorOption</td> <td>设置初始窗口大小的命令行选项。</td></tr> <tr><td>WithBrowserOption(opts ...BrowserOption) ContextOption</td> <td></td></tr> <tr><td>WithDebugf(f func(string, ...interface{})) ContextOption</td> <td></td></tr> <tr><td>WithErrorf(f func(string, ...interface{})) ContextOption</td> <td></td></tr> <tr><td>WithLogf(f func(string, ...interface{})) ContextOption</td> <td></td></tr> <tr><td>WithTargetID(id target.ID) ContextOption</td> <td></td></tr></tbody></table> <h2 id="godet"><a href="#godet" class="header-anchor">#</a> godet</h2> <ul><li><a href="https://github.com/raff/godet" target="_blank" rel="noopener noreferrer">https://github.com/raff/godet<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul> <ul><li>截图</li></ul> <div class="language-go extra-class"><pre class="language-go"><code><span class="token keyword">package</span> main

<span class="token keyword">import</span> <span class="token string">&quot;fmt&quot;</span>
<span class="token keyword">import</span> <span class="token string">&quot;time&quot;</span>

<span class="token keyword">import</span> <span class="token string">&quot;github.com/raff/godet&quot;</span>

<span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token comment">// connect to Chrome instance</span>
	remote<span class="token punctuation">,</span> err <span class="token operator">:=</span> godet<span class="token punctuation">.</span><span class="token function">Connect</span><span class="token punctuation">(</span><span class="token string">&quot;localhost:9222&quot;</span><span class="token punctuation">,</span> <span class="token boolean">false</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;cannot connect to Chrome instance:&quot;</span><span class="token punctuation">,</span> err<span class="token punctuation">)</span>
		<span class="token keyword">return</span>
	<span class="token punctuation">}</span>

	<span class="token comment">// disconnect when done</span>
	<span class="token keyword">defer</span> remote<span class="token punctuation">.</span><span class="token function">Close</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

	<span class="token comment">// get browser and protocol version</span>
	version<span class="token punctuation">,</span> <span class="token boolean">_</span> <span class="token operator">:=</span> remote<span class="token punctuation">.</span><span class="token function">Version</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>version<span class="token punctuation">)</span>

	<span class="token comment">// get list of open tabs</span>
	tabs<span class="token punctuation">,</span> <span class="token boolean">_</span> <span class="token operator">:=</span> remote<span class="token punctuation">.</span><span class="token function">TabList</span><span class="token punctuation">(</span><span class="token string">&quot;&quot;</span><span class="token punctuation">)</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>tabs<span class="token punctuation">)</span>

	<span class="token comment">// install some callbacks</span>
	remote<span class="token punctuation">.</span><span class="token function">CallbackEvent</span><span class="token punctuation">(</span>godet<span class="token punctuation">.</span>EventClosed<span class="token punctuation">,</span> <span class="token keyword">func</span><span class="token punctuation">(</span>params godet<span class="token punctuation">.</span>Params<span class="token punctuation">)</span> <span class="token punctuation">{</span>
		fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;RemoteDebugger connection terminated.&quot;</span><span class="token punctuation">)</span>
	<span class="token punctuation">}</span><span class="token punctuation">)</span>

	remote<span class="token punctuation">.</span><span class="token function">CallbackEvent</span><span class="token punctuation">(</span><span class="token string">&quot;Network.requestWillBeSent&quot;</span><span class="token punctuation">,</span> <span class="token keyword">func</span><span class="token punctuation">(</span>params godet<span class="token punctuation">.</span>Params<span class="token punctuation">)</span> <span class="token punctuation">{</span>
		fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;requestWillBeSent&quot;</span><span class="token punctuation">,</span>
			params<span class="token punctuation">[</span><span class="token string">&quot;type&quot;</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
			params<span class="token punctuation">[</span><span class="token string">&quot;documentURL&quot;</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
			params<span class="token punctuation">[</span><span class="token string">&quot;request&quot;</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token punctuation">(</span><span class="token keyword">map</span><span class="token punctuation">[</span><span class="token builtin">string</span><span class="token punctuation">]</span><span class="token keyword">interface</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token string">&quot;url&quot;</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
	<span class="token punctuation">}</span><span class="token punctuation">)</span>

	remote<span class="token punctuation">.</span><span class="token function">CallbackEvent</span><span class="token punctuation">(</span><span class="token string">&quot;Network.responseReceived&quot;</span><span class="token punctuation">,</span> <span class="token keyword">func</span><span class="token punctuation">(</span>params godet<span class="token punctuation">.</span>Params<span class="token punctuation">)</span> <span class="token punctuation">{</span>
		fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;responseReceived&quot;</span><span class="token punctuation">,</span>
			params<span class="token punctuation">[</span><span class="token string">&quot;type&quot;</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
			params<span class="token punctuation">[</span><span class="token string">&quot;response&quot;</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token punctuation">(</span><span class="token keyword">map</span><span class="token punctuation">[</span><span class="token builtin">string</span><span class="token punctuation">]</span><span class="token keyword">interface</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token string">&quot;url&quot;</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
	<span class="token punctuation">}</span><span class="token punctuation">)</span>

	remote<span class="token punctuation">.</span><span class="token function">CallbackEvent</span><span class="token punctuation">(</span><span class="token string">&quot;Log.entryAdded&quot;</span><span class="token punctuation">,</span> <span class="token keyword">func</span><span class="token punctuation">(</span>params godet<span class="token punctuation">.</span>Params<span class="token punctuation">)</span> <span class="token punctuation">{</span>
		entry <span class="token operator">:=</span> params<span class="token punctuation">[</span><span class="token string">&quot;entry&quot;</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token punctuation">(</span><span class="token keyword">map</span><span class="token punctuation">[</span><span class="token builtin">string</span><span class="token punctuation">]</span><span class="token keyword">interface</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
		fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;LOG&quot;</span><span class="token punctuation">,</span> entry<span class="token punctuation">[</span><span class="token string">&quot;type&quot;</span><span class="token punctuation">]</span><span class="token punctuation">,</span> entry<span class="token punctuation">[</span><span class="token string">&quot;level&quot;</span><span class="token punctuation">]</span><span class="token punctuation">,</span> entry<span class="token punctuation">[</span><span class="token string">&quot;text&quot;</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
	<span class="token punctuation">}</span><span class="token punctuation">)</span>

	<span class="token comment">// block loading of most images</span>
	<span class="token boolean">_</span> <span class="token operator">=</span> remote<span class="token punctuation">.</span><span class="token function">SetBlockedURLs</span><span class="token punctuation">(</span><span class="token string">&quot;*.jpg&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;*.png&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;*.gif&quot;</span><span class="token punctuation">)</span>

	<span class="token comment">// create new tab</span>
	tab<span class="token punctuation">,</span> <span class="token boolean">_</span> <span class="token operator">:=</span> remote<span class="token punctuation">.</span><span class="token function">NewTab</span><span class="token punctuation">(</span><span class="token string">&quot;https://www.google.com&quot;</span><span class="token punctuation">)</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>tab<span class="token punctuation">)</span>

	<span class="token comment">// enable event processing</span>
	remote<span class="token punctuation">.</span><span class="token function">RuntimeEvents</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span>
	remote<span class="token punctuation">.</span><span class="token function">NetworkEvents</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span>
	remote<span class="token punctuation">.</span><span class="token function">PageEvents</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span>
	remote<span class="token punctuation">.</span><span class="token function">DOMEvents</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span>
	remote<span class="token punctuation">.</span><span class="token function">LogEvents</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span>

	<span class="token comment">// navigate in existing tab</span>
	<span class="token boolean">_</span> <span class="token operator">=</span> remote<span class="token punctuation">.</span><span class="token function">ActivateTab</span><span class="token punctuation">(</span>tabs<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span>

	<span class="token comment">//remote.StartPreciseCoverage(true, true)</span>

	<span class="token comment">// re-enable events when changing active tab</span>
	remote<span class="token punctuation">.</span><span class="token function">AllEvents</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span> <span class="token comment">// enable all events</span>

	<span class="token boolean">_</span><span class="token punctuation">,</span> <span class="token boolean">_</span> <span class="token operator">=</span> remote<span class="token punctuation">.</span><span class="token function">Navigate</span><span class="token punctuation">(</span><span class="token string">&quot;https://www.google.com&quot;</span><span class="token punctuation">)</span>

	<span class="token comment">// evaluate Javascript expression in existing context</span>
	res<span class="token punctuation">,</span> <span class="token boolean">_</span> <span class="token operator">:=</span> remote<span class="token punctuation">.</span><span class="token function">EvaluateWrap</span><span class="token punctuation">(</span><span class="token string">`
            console.log(&quot;hello from godet!&quot;)
            return 42;
        `</span><span class="token punctuation">)</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>res<span class="token punctuation">)</span>

	<span class="token comment">// take a screenshot</span>
	<span class="token boolean">_</span> <span class="token operator">=</span> remote<span class="token punctuation">.</span><span class="token function">SaveScreenshot</span><span class="token punctuation">(</span><span class="token string">&quot;screenshot.png&quot;</span><span class="token punctuation">,</span> <span class="token number">0644</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token boolean">true</span><span class="token punctuation">)</span>

	time<span class="token punctuation">.</span><span class="token function">Sleep</span><span class="token punctuation">(</span>time<span class="token punctuation">.</span>Second<span class="token punctuation">)</span>

	<span class="token comment">// or save page as PDF</span>
	<span class="token boolean">_</span> <span class="token operator">=</span> remote<span class="token punctuation">.</span><span class="token function">SavePDF</span><span class="token punctuation">(</span><span class="token string">&quot;page.pdf&quot;</span><span class="token punctuation">,</span> <span class="token number">0644</span><span class="token punctuation">,</span> godet<span class="token punctuation">.</span><span class="token function">PortraitMode</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
            godet<span class="token punctuation">.</span><span class="token function">Scale</span><span class="token punctuation">(</span><span class="token number">0.5</span><span class="token punctuation">)</span><span class="token punctuation">,</span> godet<span class="token punctuation">.</span><span class="token function">Dimensions</span><span class="token punctuation">(</span><span class="token number">6.0</span><span class="token punctuation">,</span> <span class="token number">2.0</span><span class="token punctuation">)</span><span class="token punctuation">)</span>

	<span class="token comment">// if err := remote.SetInputFiles(0, []string{&quot;hello.txt&quot;}); err != nil {</span>
	<span class="token comment">//     fmt.Println(&quot;setInputFiles&quot;, err)</span>
	<span class="token comment">// }</span>

	time<span class="token punctuation">.</span><span class="token function">Sleep</span><span class="token punctuation">(</span><span class="token number">5</span> <span class="token operator">*</span> time<span class="token punctuation">.</span>Second<span class="token punctuation">)</span>

	<span class="token comment">//remote.StopPreciseCoverage()</span>

	r<span class="token punctuation">,</span> err <span class="token operator">:=</span> remote<span class="token punctuation">.</span><span class="token function">GetPreciseCoverage</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;error profiling&quot;</span><span class="token punctuation">,</span> err<span class="token punctuation">)</span>
	<span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
		fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>r<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>

	<span class="token comment">// Allow downloads</span>
	<span class="token boolean">_</span> <span class="token operator">=</span> remote<span class="token punctuation">.</span><span class="token function">SetDownloadBehavior</span><span class="token punctuation">(</span>godet<span class="token punctuation">.</span>AllowDownload<span class="token punctuation">,</span> <span class="token string">&quot;/tmp/&quot;</span><span class="token punctuation">)</span>
	<span class="token boolean">_</span><span class="token punctuation">,</span> <span class="token boolean">_</span> <span class="token operator">=</span> remote<span class="token punctuation">.</span><span class="token function">Navigate</span><span class="token punctuation">(</span>url<span class="token punctuation">)</span>

	time<span class="token punctuation">.</span><span class="token function">Sleep</span><span class="token punctuation">(</span>time<span class="token punctuation">.</span>Second<span class="token punctuation">)</span>

	<span class="token comment">// Block downloads</span>
	<span class="token boolean">_</span> <span class="token operator">=</span> remote<span class="token punctuation">.</span><span class="token function">SetDownloadBehavior</span><span class="token punctuation">(</span>godet<span class="token punctuation">.</span>DenyDownload<span class="token punctuation">,</span> <span class="token string">&quot;&quot;</span><span class="token punctuation">)</span>
	<span class="token boolean">_</span><span class="token punctuation">,</span> <span class="token boolean">_</span> <span class="token operator">=</span> remote<span class="token punctuation">.</span><span class="token function">Navigate</span><span class="token punctuation">(</span>url<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre></div><h2 id="gcd"><a href="#gcd" class="header-anchor">#</a> gcd</h2> <ul><li><a href="https://github.com/wirepair/gcd" target="_blank" rel="noopener noreferrer">https://github.com/wirepair/gcd<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul> <ul><li>截图</li></ul> <div class="language-go extra-class"><pre class="language-go"><code><span class="token keyword">package</span> main

<span class="token keyword">import</span> <span class="token punctuation">(</span>
	<span class="token string">&quot;encoding/base64&quot;</span>
	<span class="token string">&quot;flag&quot;</span>
	<span class="token string">&quot;fmt&quot;</span>
	<span class="token string">&quot;log&quot;</span>
	<span class="token string">&quot;net/url&quot;</span>
	<span class="token string">&quot;os&quot;</span>
	<span class="token string">&quot;runtime&quot;</span>
	<span class="token string">&quot;sync&quot;</span>
	<span class="token string">&quot;time&quot;</span>

	<span class="token string">&quot;github.com/wirepair/gcd&quot;</span>
	<span class="token string">&quot;github.com/wirepair/gcd/gcdapi&quot;</span>
<span class="token punctuation">)</span>

<span class="token keyword">const</span> <span class="token punctuation">(</span>
	numTabs <span class="token operator">=</span> <span class="token number">5</span>
<span class="token punctuation">)</span>

<span class="token keyword">var</span> debugger <span class="token operator">*</span>gcd<span class="token punctuation">.</span>Gcd
<span class="token keyword">var</span> wg sync<span class="token punctuation">.</span>WaitGroup

<span class="token keyword">var</span> path <span class="token builtin">string</span>
<span class="token keyword">var</span> dir <span class="token builtin">string</span>
<span class="token keyword">var</span> port <span class="token builtin">string</span>

<span class="token keyword">func</span> <span class="token function">init</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token keyword">switch</span> runtime<span class="token punctuation">.</span>GOOS <span class="token punctuation">{</span>
	<span class="token keyword">case</span> <span class="token string">&quot;windows&quot;</span><span class="token punctuation">:</span>
		flag<span class="token punctuation">.</span><span class="token function">StringVar</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>path<span class="token punctuation">,</span> <span class="token string">&quot;chrome&quot;</span><span class="token punctuation">,</span>
            <span class="token string">&quot;C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe&quot;</span><span class="token punctuation">,</span>
            <span class="token string">&quot;path to chrome&quot;</span><span class="token punctuation">)</span>
		flag<span class="token punctuation">.</span><span class="token function">StringVar</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>dir<span class="token punctuation">,</span> <span class="token string">&quot;dir&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;C:\\temp\\&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;user directory&quot;</span><span class="token punctuation">)</span>
	<span class="token keyword">case</span> <span class="token string">&quot;darwin&quot;</span><span class="token punctuation">:</span>
		flag<span class="token punctuation">.</span><span class="token function">StringVar</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>path<span class="token punctuation">,</span> <span class="token string">&quot;chrome&quot;</span><span class="token punctuation">,</span>
            <span class="token string">&quot;/Applications/Google Chrome.app/Contents/MacOS/Google Chrome&quot;</span><span class="token punctuation">,</span>
            <span class="token string">&quot;path to chrome&quot;</span><span class="token punctuation">)</span>
		flag<span class="token punctuation">.</span><span class="token function">StringVar</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>dir<span class="token punctuation">,</span> <span class="token string">&quot;dir&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;/tmp/&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;user directory&quot;</span><span class="token punctuation">)</span>
	<span class="token keyword">case</span> <span class="token string">&quot;linux&quot;</span><span class="token punctuation">:</span>
		flag<span class="token punctuation">.</span><span class="token function">StringVar</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>path<span class="token punctuation">,</span> <span class="token string">&quot;chrome&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;/usr/bin/chromium-browser&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;path to chrome&quot;</span><span class="token punctuation">)</span>
		flag<span class="token punctuation">.</span><span class="token function">StringVar</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>dir<span class="token punctuation">,</span> <span class="token string">&quot;dir&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;/tmp/&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;user directory&quot;</span><span class="token punctuation">)</span>
	<span class="token punctuation">}</span>

	flag<span class="token punctuation">.</span><span class="token function">StringVar</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>port<span class="token punctuation">,</span> <span class="token string">&quot;port&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;9222&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;Debugger port&quot;</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>

<span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token keyword">var</span> err <span class="token builtin">error</span>
	urls <span class="token operator">:=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">string</span><span class="token punctuation">{</span><span class="token string">&quot;http://www.google.com&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;http://www.veracode.com&quot;</span><span class="token punctuation">,</span>
            <span class="token string">&quot;http://www.microsoft.com&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;http://bbc.co.uk&quot;</span><span class="token punctuation">,</span>
            <span class="token string">&quot;http://www.reddit.com/r/golang&quot;</span><span class="token punctuation">}</span>

	flag<span class="token punctuation">.</span><span class="token function">Parse</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

	debugger <span class="token operator">=</span> gcd<span class="token punctuation">.</span><span class="token function">NewChromeDebugger</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	debugger<span class="token punctuation">.</span><span class="token function">StartProcess</span><span class="token punctuation">(</span>path<span class="token punctuation">,</span> dir<span class="token punctuation">,</span> port<span class="token punctuation">)</span>
	<span class="token keyword">defer</span> debugger<span class="token punctuation">.</span><span class="token function">ExitProcess</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

	targets <span class="token operator">:=</span> <span class="token function">make</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token operator">*</span>gcd<span class="token punctuation">.</span>ChromeTarget<span class="token punctuation">,</span> numTabs<span class="token punctuation">)</span>

	<span class="token keyword">for</span> i <span class="token operator">:=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> numTabs<span class="token punctuation">;</span> i<span class="token operator">++</span> <span class="token punctuation">{</span>
		wg<span class="token punctuation">.</span><span class="token function">Add</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span>
		targets<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">,</span> err <span class="token operator">=</span> debugger<span class="token punctuation">.</span><span class="token function">NewTab</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
		<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
			log<span class="token punctuation">.</span><span class="token function">Fatalf</span><span class="token punctuation">(</span><span class="token string">&quot;error getting targets&quot;</span><span class="token punctuation">)</span>
		<span class="token punctuation">}</span>
		page <span class="token operator">:=</span> targets<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>Page
		page<span class="token punctuation">.</span><span class="token function">Enable</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
		targets<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">Subscribe</span><span class="token punctuation">(</span><span class="token string">&quot;Page.loadEventFired&quot;</span><span class="token punctuation">,</span> pageLoaded<span class="token punctuation">)</span>
		<span class="token comment">// navigate</span>
		navigateParams <span class="token operator">:=</span> <span class="token operator">&amp;</span>gcdapi<span class="token punctuation">.</span>PageNavigateParams<span class="token punctuation">{</span>Url<span class="token punctuation">:</span> urls<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">}</span>
		<span class="token boolean">_</span><span class="token punctuation">,</span> <span class="token boolean">_</span><span class="token punctuation">,</span> <span class="token boolean">_</span><span class="token punctuation">,</span> err <span class="token operator">:=</span> page<span class="token punctuation">.</span><span class="token function">NavigateWithParams</span><span class="token punctuation">(</span>navigateParams<span class="token punctuation">)</span>
		<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
			log<span class="token punctuation">.</span><span class="token function">Fatalf</span><span class="token punctuation">(</span><span class="token string">&quot;error: %s\n&quot;</span><span class="token punctuation">,</span> err<span class="token punctuation">)</span>
		<span class="token punctuation">}</span>
	<span class="token punctuation">}</span>
	wg<span class="token punctuation">.</span><span class="token function">Wait</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">for</span> i <span class="token operator">:=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> numTabs<span class="token punctuation">;</span> i<span class="token operator">++</span> <span class="token punctuation">{</span>
		<span class="token function">takeScreenShot</span><span class="token punctuation">(</span>targets<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token keyword">func</span> <span class="token function">pageLoaded</span><span class="token punctuation">(</span>target <span class="token operator">*</span>gcd<span class="token punctuation">.</span>ChromeTarget<span class="token punctuation">,</span> event <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	target<span class="token punctuation">.</span><span class="token function">Unsubscribe</span><span class="token punctuation">(</span><span class="token string">&quot;Page.loadEventFired&quot;</span><span class="token punctuation">)</span>
	wg<span class="token punctuation">.</span><span class="token function">Done</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>

<span class="token keyword">func</span> <span class="token function">takeScreenShot</span><span class="token punctuation">(</span>target <span class="token operator">*</span>gcd<span class="token punctuation">.</span>ChromeTarget<span class="token punctuation">)</span> <span class="token punctuation">{</span>
	dom <span class="token operator">:=</span> target<span class="token punctuation">.</span>DOM
	page <span class="token operator">:=</span> target<span class="token punctuation">.</span>Page
	doc<span class="token punctuation">,</span> err <span class="token operator">:=</span> dom<span class="token punctuation">.</span><span class="token function">GetDocument</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token boolean">true</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">&quot;error getting doc: %s\n&quot;</span><span class="token punctuation">,</span> err<span class="token punctuation">)</span>
		<span class="token keyword">return</span>
	<span class="token punctuation">}</span>

	debugger<span class="token punctuation">.</span><span class="token function">ActivateTab</span><span class="token punctuation">(</span>target<span class="token punctuation">)</span>
	time<span class="token punctuation">.</span><span class="token function">Sleep</span><span class="token punctuation">(</span><span class="token number">1</span> <span class="token operator">*</span> time<span class="token punctuation">.</span>Second<span class="token punctuation">)</span> <span class="token comment">// give it a sec to paint</span>
	u<span class="token punctuation">,</span> urlErr <span class="token operator">:=</span> url<span class="token punctuation">.</span><span class="token function">Parse</span><span class="token punctuation">(</span>doc<span class="token punctuation">.</span>DocumentURL<span class="token punctuation">)</span>
	<span class="token keyword">if</span> urlErr <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">&quot;error parsing url: %s\n&quot;</span><span class="token punctuation">,</span> urlErr<span class="token punctuation">)</span>
		<span class="token keyword">return</span>
	<span class="token punctuation">}</span>

	fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">&quot;Taking screen shot of: %s\n&quot;</span><span class="token punctuation">,</span> u<span class="token punctuation">.</span>Host<span class="token punctuation">)</span>
	screenShotParams <span class="token operator">:=</span> <span class="token operator">&amp;</span>gcdapi<span class="token punctuation">.</span>PageCaptureScreenshotParams<span class="token punctuation">{</span>Format<span class="token punctuation">:</span> <span class="token string">&quot;png&quot;</span><span class="token punctuation">,</span>
                                FromSurface<span class="token punctuation">:</span> <span class="token boolean">true</span><span class="token punctuation">}</span>
	img<span class="token punctuation">,</span> errCap <span class="token operator">:=</span> page<span class="token punctuation">.</span><span class="token function">CaptureScreenshotWithParams</span><span class="token punctuation">(</span>screenShotParams<span class="token punctuation">)</span>
	<span class="token keyword">if</span> errCap <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">&quot;error getting doc: %s\n&quot;</span><span class="token punctuation">,</span> errCap<span class="token punctuation">)</span>
		<span class="token keyword">return</span>
	<span class="token punctuation">}</span>

	imgBytes<span class="token punctuation">,</span> errDecode <span class="token operator">:=</span> base64<span class="token punctuation">.</span>StdEncoding<span class="token punctuation">.</span><span class="token function">DecodeString</span><span class="token punctuation">(</span>img<span class="token punctuation">)</span>
	<span class="token keyword">if</span> errDecode <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">&quot;error decoding image: %s\n&quot;</span><span class="token punctuation">,</span> errDecode<span class="token punctuation">)</span>
		<span class="token keyword">return</span>
	<span class="token punctuation">}</span>

	f<span class="token punctuation">,</span> errFile <span class="token operator">:=</span> os<span class="token punctuation">.</span><span class="token function">Create</span><span class="token punctuation">(</span>u<span class="token punctuation">.</span>Host <span class="token operator">+</span> <span class="token string">&quot;.png&quot;</span><span class="token punctuation">)</span>
	<span class="token keyword">defer</span> f<span class="token punctuation">.</span><span class="token function">Close</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> errFile <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">&quot;error creating image file: %s\n&quot;</span><span class="token punctuation">,</span> errFile<span class="token punctuation">)</span>
		<span class="token keyword">return</span>
	<span class="token punctuation">}</span>
	f<span class="token punctuation">.</span><span class="token function">Write</span><span class="token punctuation">(</span>imgBytes<span class="token punctuation">)</span>
	debugger<span class="token punctuation">.</span><span class="token function">CloseTab</span><span class="token punctuation">(</span>target<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre></div></div> <footer class="page-edit"><div class="edit-link"><a href="https://github.com/woytu/notes-vuepress/edit/master//Form/Go爬虫.md" target="_blank" rel="noopener noreferrer">在 GitHub 上编辑此页</a> <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></div> <!----></footer> <div class="page-nav"><p class="inner"><span class="prev">
      ←
      <a href="/Form/Go操作Windows.html" class="prev">
        Go操作Windows
      </a></span> <span class="next"><a href="/Form/Go笔记.html">
        Go笔记
      </a>
      →
    </span></p></div> </main></div><div class="global-ui"><!----></div></div>
    <script src="/assets/js/app.269009d6.js" defer></script><script src="/assets/js/3.974b9720.js" defer></script><script src="/assets/js/9.ad652cfe.js" defer></script>
  </body>
</html>
